package Leo.controller;

import Leo.dao.SystemsettingDao;
import Leo.entity.param.Socialinfo;
import Leo.entity.table.Systemsetting;
import Leo.fileIO.FileDownload;
import Leo.fileIO.FileUpload;
import Leo.redis.RedisUtil;
import Leo.service.RedisService;
import Leo.utils.CtmSmsUtil;
import Leo.utils.FormatTrans;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/base")
//设置作用域为单例模式
@Scope("singleton")
public class BaseController {

    @Autowired
    FileUpload fileUpload;

    @Autowired
    FileDownload fileDownload;

    @Autowired
    RedisService redisService;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    CtmSmsUtil ctmSmsUtil;

    @Autowired
    SystemsettingDao systemsettingDao;

    @Autowired
    ObjectMapper objectMapper;

    @RequestMapping("/info")
    public String getUserinfo(Long userId) {
        return "hello ,TA";
    }

    @RequestMapping("/index")
    public String index() {
        return "hello ,this is index templates ,this templates permit all visit";
    }

    @RequestMapping("/upload")
    public String upload(MultipartFile file, String suffix) throws IOException {
        long l = fileUpload.uploadFile(file, "e:/upload", suffix);
        return "success!cost :" + l + "milliseconds";
    }

    @RequestMapping("/uploadFiles")
    public String uploadFiles(List<MultipartFile> file) throws IOException {
        long l = fileUpload.uploadFile(file, "e:/upload");
        return "success!cost :" + l + "milliseconds";
    }

    @RequestMapping("/downloadFile")
    public HttpServletResponse downloadFile(String path, HttpServletResponse response) throws IOException {
        fileDownload.downFile(path, response);
        return response;
    }

    @RequestMapping("/showImage")
    public HttpServletResponse showImage(String path, HttpServletResponse response) throws IOException {
        fileDownload.transFile(path, response);
        return response;
    }

    @RequestMapping("/formatTrans")
    public String formatTrans(String param, String type) throws IOException {
        String result = param + " | " + type;
//        if ("J".equals(type)) {
//            result = FormatTrans.jsonToXmlByJsonLib(param);
//        } else {
//            result = FormatTrans.xmlToJsonByJsonLib(param);
//        }
        return result;
    }

    @RequestMapping("/{id}")
    public String idTest(@PathVariable("id") String id, @RequestParam("key") String redisKey, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("ID:" + id);
        return id;
    }

    @RequestMapping("/entity")
    public String formatTrans(@RequestBody Socialinfo socialinfo) throws IOException {
        String s = objectMapper.writeValueAsString(socialinfo);
        System.out.println(s);
        return s;
    }

    @RequestMapping("/saveRedis/{key}/{value}")
    public String saveRedis(@PathVariable("key") String key, @PathVariable("value") String value) throws IOException {
        if (StringUtils.isNotBlank(key)) {
            redisService.saveValue(key, value);
        }
        return "saved";
    }

    @RequestMapping("/getRedis")
    public Object getRedis(@RequestParam("key") String redisKey) throws IOException {
        String value = redisService.getValue(redisKey).toString();
        return value;
    }

    @RequestMapping("/getStringRedis")
    public String getStringRedis(@RequestParam("key") String redisKey) throws IOException {
        String value = redisService.getStringValue(redisKey);
        return value;
    }

    @RequestMapping("/putRedisMap")
    public Object saveRedisMap(Long id) throws JsonProcessingException {
        Object value = redisUtil.putByStringRedis(id);
        return value;
    }

    @RequestMapping("/getRedisMap")
    public Object getRedisMap(Long id)  {
        Object byStringRedis = redisUtil.getByStringRedis(id);
        return byStringRedis;
    }

    @RequestMapping("/putRedisMap1")
    public Object saveRedisMap1(Long id) {
        Object value = redisUtil.putByRedis(id);
        return value;
    }

    @RequestMapping("/getRedisMap1")
    public Object getRedisMap1(Long id)  {
        Object byStringRedis = redisUtil.getByRedis(id);
        return byStringRedis;
    }

    @RequestMapping("/putCache")
    public Object putCache(String value2)  {
        Systemsetting systemsetting = redisService.cacheFind(1L,"redis_database");
        systemsetting.setSettingValue2(value2);
        Object byStringRedis = redisService.cacheSave(systemsetting);
        return byStringRedis;
    }
    @RequestMapping("/findCache")
    public Object findCache(Long id)  {
        Systemsetting systemsetting = redisService.cacheFind(id,"redis_database");
        return systemsetting;
    }
    @RequestMapping("/delCache")
    public Object delCache(Long id)  {
        redisService.cacheRemove(id,"redis_database");
        return "remove";
    }

    @RequestMapping("/sendSms")
    public String formatTrans2(String phone, String cnName, String enName, String cnDr, String enDr, String type) throws IOException {
        String result = "";
        System.out.println("发短信");
        if ("R".equals(type)) {
            result = ctmSmsUtil.sendSms(phone, cnName, enName, cnDr, enDr, new Date(), CtmSmsUtil.APPT_REMINDER);
        } else {
            result = ctmSmsUtil.sendSms(phone, cnName, enName, cnDr, enDr, new Date(), CtmSmsUtil.APPT_SUCCESS);
        }
        return result;
    }

    @RequestMapping("/sendSms2")
    public String formatTrans2(String phone, String content) throws IOException {
        String result = "";
        System.out.println("发短信");
        result = ctmSmsUtil.sendSms(phone, content);

        return result;
    }

    @RequestMapping("/querySetting")
    public Systemsetting querySetting(String settingCode){
        Systemsetting systemsetting = systemsettingDao.selectBySettingCode(settingCode);
        return systemsetting;
    }
}
